const express = require('express')
const app = express()
const port = 3000
const fs = require('fs')
const puppeteer = require('puppeteer')
const cheerio = require('cheerio');
const superagent = require('superagent')
const xlsx = require('node-xlsx')

const url = 'https://mp.weixin.qq.com/s?__biz=MzkwMzI3MzcwMw==&mid=2247488282&idx=1&sn=95a52c44b413ab26b4e63ab8dd896a2b&source=41#wechat_redirect'
// const url = 'https://segmentfault.com/a/1190000014811373'
const arr = require('./data/base_data.json')
let num = 0

const WXData = []

const getWXData = (url) => {

        return new Promise(async(ress, rej) => {
            try {
                let res = await superagent.get(url)
                let $ = cheerio.load(res.text)
                let obj = {
                    title: $('meta[property="og:title"]').attr('content'),
                    banner: $('meta[property="og:image"]').attr('content'),
                    link: url
                }
                ress(obj)
            } catch (error) {
                rej(false)
                console.log(error);
            }
        })
}
// superagent.get(url).end( async (err, res) => {
//     // console.log(res);
//     let $ = cheerio.load(res.text)
//     let obj = {
//         title: '',
//         img: ''
//     }
//     $('meta[property="og:title"]').each((i, ele) => {
//         obj.title = $(ele).attr('content')
//     })
//     $('meta[property="og:image"]').each((i, ele) => {
//         obj.img = $(ele).attr('content')
//     })
//     obj.link = url
//    WXData.push(obj)
// })

const sleep = () => new Promise(res => {
    setTimeout(res), 1000
})

const handleArr = (base_arr) => {
  const datas = []
  const titles = ['title', 'banner', 'link']
  datas.push(titles)

  base_arr.forEach(v => {
    const arrInner = []
    titles.forEach(title => arrInner.push(v[title]))
    datas.push(arrInner)
  })
  return datas
}

const timer = setInterval(async() => {
    try {
        if(num === arr.length) {
            // console.log(WXData);
            const buffer = xlsx.build([
              {
                name: 'sheet1',
                data: handleArr(WXData)
              }
            ])
            console.log('over');
            let a = fs.writeFileSync('./data/result.xlsx', buffer)
            return clearInterval(timer)
        }
        
        let val = await getWXData(arr[num].link)
        console.log(val);
        val && WXData.push(val)
        num++
    } catch (error) {
        console.log(error);
    }
}, 300)

app.get('/', async (req, res) => {
    
    res.send('11')
})
app.listen(port, () => console.log(`https://localhost:3000`))